package com.zhuojy.equity.mall.admin.web.controller;

import com.zhuojy.equity.mall.admin.common.utils.FtpUtil;
import com.zhuojy.equity.mall.admin.common.utils.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/ftp")
@RequiredArgsConstructor
@Api(tags = "上下",value = "上下" )
public class FtpController {

    private final FtpUtil ftpUtil;

    private static final String ROOT_DIR = "/";

    private static final  String IMG_URl= "https://rx.133.w21.net/";

    @GetMapping("/getFileNameList")
    public List<String> getFileNameList() {
        return ftpUtil.getFileNameList(ROOT_DIR);
    }

    @RequestMapping("/upload")
    @ApiOperation(value = "上传",httpMethod = "POST")
    public String uploadToFtpServer(MultipartFile file) throws IOException {
        // 读取文件信息
        String fileName = file.getOriginalFilename();
        int i = fileName.lastIndexOf(".");
        String substring = fileName.substring(i);
        UUID uuid = UUID.randomUUID();
        fileName=uuid.toString()+substring;
        InputStream fileInputStream = file.getInputStream();
        if (ftpUtil.upload(ROOT_DIR, fileName, fileInputStream)){
            return IMG_URl+fileName;
        }
        // 上传文件到 Ftp 服务
        return "上传失败";
    }

    @GetMapping("/download")
    @ApiOperation(value = "下载",httpMethod = "GET")
    public boolean download(){
        String localPath = "Z/", ftpFileName = "1.png";
        return ftpUtil.download(ROOT_DIR, ftpFileName, localPath);
    }

    @GetMapping("/delete")
    public boolean delete(String fileName){
        return ftpUtil.delete(ROOT_DIR, fileName);
    }
}
